package mt.client.tests;

import static org.junit.Assert.*;
import mt.Order;
import mt.client.GUI;
import mt.dummy.ClientCommDummy;
import org.junit.*;

public class Connection_TEST {

	@Test
	public void connectionIsOpenTest(){
		GUI gui = new GUI();
		gui.connect();
		assertTrue(gui.getComm().isConnected());
	}

	@Test
	public void connectionIsCloseTest(){
		GUI gui = new GUI();
		assertFalse(gui.getComm().isConnected());
	}

	@Test
	public void connectionIsCloseAfterDisconnectTest(){
		GUI gui = new GUI();
		gui.disconnect();
		assertFalse(gui.getComm().isConnected());
	}
	
	@Test
	public void connectionIsCloseAfterConnectDisconnectTest(){
		GUI gui = new GUI();
		gui.connect();
		gui.disconnect();
		assertFalse(gui.getComm().isConnected());
	}
	
	@Test
	public void hasOrdersAfterOneBuyTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order order = Order.createBuyOrder("testNick", "bananas", 1, 1);
		gui.getComm().sendOrder(order);
		Thread.sleep(1000);
		assertTrue(gui.getOrders().size()>0);
		assertTrue(gui.getOrders().pollFirst().equals(order));
	}
	
	@Test
	public void hasOrdersAfterOneSellTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order order = Order.createSellOrder("testNick", "bananas", 1, 1);
		gui.getComm().sendOrder(order);
		Thread.sleep(1000);
		assertTrue(gui.getOrders().size()==1);
		assertTrue(gui.getOrders().pollFirst().equals(order));
	}

	@Test
	public void doesntHaveOrdersAfterReceivingSameOrderWithNumberOfUnitsZero() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order buyOrder = Order.createBuyOrder("testNick", "bananas", 1, 1);
		buyOrder.setServerOrderID(1);
		Order newBuyOrder = Order.createBuyOrder("testNick", "bananas", 0, 0);
		newBuyOrder.setServerOrderID(1);
		gui.getComm().sendOrder(buyOrder);
		gui.getComm().sendOrder(newBuyOrder);
		Thread.sleep(1000);
		assertTrue(gui.getOrders().size()==0);
	}
	
	@Test
	public void hasTwoOrdersAfterBuyAndSellSameObjectsWithDifferentValuesTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order buyOrder = Order.createBuyOrder("testNick", "bananas", 1, 1);
		buyOrder.setServerOrderID(1);
		Order sellOrder = Order.createSellOrder("testNick", "bananas", 0, 0);
		sellOrder.setServerOrderID(2);
		gui.getComm().sendOrder(buyOrder);
		gui.getComm().sendOrder(sellOrder);
		Thread.sleep(1000);
		assertTrue(gui.getOrders().size()==2);
	}
	
	@Test
	public void hasCorrectNicknameTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Thread.sleep(1000);
		gui.setNickname("Ola");
		assertTrue(gui.getNickname().equals("Ola"));
	}
	
	@Test
	public void displaysCorrectOrderDataTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order buyOrder = Order.createBuyOrder("testNick", "bananas", 1, 1);
		buyOrder.setServerOrderID(1);
		gui.getComm().sendOrder(buyOrder);
		Thread.sleep(1000);
		Order order = gui.getOrders().poll();
		assertTrue(order.getNickname().equals("testNick"));
		assertTrue(order.getNumberOfUnits()==1);
		assertTrue(order.getPricePerUnit()==1);
		assertTrue(order.getServerOrderID()==1);
		assertTrue(order.getStock().equals("bananas"));
	}
	
	@Test
	public void assertErrorMessageIsDiscardedTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		//gui.getComm().queueErrorMessage();
		Thread.sleep(1000);
		assertTrue(gui.getOrders().size()==0);
	}
	
	@Test
	public void assertSamerOrderWithNumberOfUnitsGreaterThanZeroisChangedTest() throws InterruptedException{
		GUI gui = new GUI();
		gui.connect();
		Order buyOrder = Order.createBuyOrder("testNick", "bananas", 1, 1);
		buyOrder.setServerOrderID(1);
		Order newBuyOrder = Order.createBuyOrder("testNick", "bananas", 2, 1);
		newBuyOrder.setServerOrderID(1);
		Thread.sleep(500);
		gui.getComm().sendOrder(buyOrder);
		Thread.sleep(500);
		gui.getComm().sendOrder(newBuyOrder);
		Thread.sleep(500);
		assertTrue(gui.getOrders().size()>0);
		Order order = gui.getOrders().poll();
		System.out.println(order);
		assertTrue(order.getNickname().equals("testNick"));
		assertTrue(order.getNumberOfUnits()==2);
		assertTrue(order.getPricePerUnit()==1);
		assertTrue(order.getStock().equals("bananas"));
		assertTrue(order.getServerOrderID()==1);
	}
	
}